package io.renren.sf.controller;

import java.util.Arrays;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import io.renren.sf.entity.CbPracticeEntity;
import io.renren.sf.service.CbPracticeService;
import io.renren.common.utils.R;



/**
 *
 *
 * @author cyl_txw
 * @email cyl_txw@gmail.com
 * @date 2021-04-29 13:03:40
 */
@RestController
@RequestMapping("sf/cbpractice")
public class CbPracticeController {
    @Autowired
    private CbPracticeService cbPracticeService;

    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    //@RequiresPermissions("sf:cbpractice:info")
    public R info(@PathVariable("id") Long id){
		CbPracticeEntity cbPractice = cbPracticeService.getById(id);
        return R.ok().put("cbPractice", cbPractice);
    }

    @GetMapping("/getPracticeByWordId/{tid}")
    //@RequiresPermissions("sf:cbpractice:info")
    public R getPracticeByWordId(@PathVariable("tid") Long tid){
        CbPracticeEntity cbPractice = cbPracticeService.getOne(new QueryWrapper<CbPracticeEntity>().eq("tid", tid).last("limit 1"), false);
        return R.ok().put("cbPractice", cbPractice);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    //@RequiresPermissions("sf:cbpractice:save")
    public R save(@RequestBody CbPracticeEntity cbPractice){
        try {
            cbPracticeService.save(cbPractice);
            return R.ok();
        } catch (Exception e) {
            return R.error("该字体已存在练习数据，请刷新页面");
        }
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    //@RequiresPermissions("sf:cbpractice:update")
    public R update(@RequestBody CbPracticeEntity cbPractice){
		cbPracticeService.updateById(cbPractice);

        return R.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    //@RequiresPermissions("sf:cbpractice:delete")
    public R delete(@RequestBody Long[] ids){
		cbPracticeService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
